package com.example.sunmiv1.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.sunmiv1.R;
import com.example.sunmiv1.entity.GoodsPriceInfo;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GoodsPriceAdapter extends BaseAdapter {

    private Context context;
    private ViewHolder viewHolder;
    private OnListener onListener;
    private List<GoodsPriceInfo.GoodsListBean> list;

    public GoodsPriceAdapter(Context context, List<GoodsPriceInfo.GoodsListBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.goods_price_adapter, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        Resources resources = context.getResources();
        /*品名*/
        viewHolder.priceQueryName.setText(resources.getString(R.string.priceQuery_name) + list.get(position).getGoods_name());
        /*規格*/
        if ("1".equals(list.get(position).getAttribute_state())) {//不是規格商品
            viewHolder.linearSku.setVisibility(View.GONE);
            viewHolder.viewSku.setVisibility(View.GONE);
        } else {//規格商品
            viewHolder.linearSku.setVisibility(View.VISIBLE);
            viewHolder.viewSku.setVisibility(View.VISIBLE);
            viewHolder.skuName.setText(resources.getString(R.string.priceQuery_skuName) + list.get(position).getSku_name());
        }
        /*貨號*/
        if (list.get(position).getGoods_number() == null || TextUtils.isEmpty(list.get(position).getGoods_number())) {
            viewHolder.priceQueryFreightNumber.setText(resources.getString(R.string.priceQuery_freightNumber) + context.getString(R.string.goodsPriceTip1));
        } else {
            viewHolder.priceQueryFreightNumber.setText(resources.getString(R.string.priceQuery_freightNumber) + list.get(position).getGoods_number());
        }
        /*商品條碼*/
        if (list.get(position).getGoods_code() == null || TextUtils.isEmpty(list.get(position).getGoods_code())) {
            viewHolder.priceQueryCode.setText(resources.getString(R.string.priceQuery_code) + context.getString(R.string.goodsPriceTip2));
        } else {
            viewHolder.priceQueryCode.setText(resources.getString(R.string.priceQuery_code) + list.get(position).getGoods_code());
        }
        /*店內零售價*/
        viewHolder.priceQueryStoreRetail.setText(resources.getString(R.string.priceQuery_storeRetail) + list.get(position).getRetail_price());
        /*店內會員價*/
        if (list.get(position).getStore_mem_status().equals("1")) {
            viewHolder.linearStoreMember.setVisibility(View.VISIBLE);
            viewHolder.priceQueryStoreMember.setText(resources.getString(R.string.priceQuery_storeMember) + list.get(position).getStore_mem_price());
        } else {
            viewHolder.linearStoreMember.setVisibility(View.GONE);
        }
        /*商城價*/
        if (list.get(position).getOnline_state().equals("1")) {
            viewHolder.linearMall.setVisibility(View.VISIBLE);
            viewHolder.priceQueryMall.setText(resources.getString(R.string.priceQuery_mall) + list.get(position).getOnline_price());
        } else {
            viewHolder.linearMall.setVisibility(View.GONE);
        }
        /*商城會員價*/
        if (list.get(position).getShop_mem_status().equals("1")) {
            viewHolder.linearMallMember.setVisibility(View.VISIBLE);
            viewHolder.priceQueryMallMember.setText(resources.getString(R.string.priceQuery_mallMember) + list.get(position).getShop_mem_price());
        } else {
            viewHolder.linearMallMember.setVisibility(View.GONE);
        }
        /*PLUS價*/
        if (list.get(position).getPlus_mem_status().equals("1")) {
            viewHolder.linearPlus.setVisibility(View.VISIBLE);
            viewHolder.priceQueryPlus.setText(resources.getString(R.string.priceQuery_plus) + list.get(position).getPlus_price());
        } else {
            viewHolder.linearPlus.setVisibility(View.GONE);
        }
        final int index = position;
        final TextView changePrice = viewHolder.priceQueryChangePrice;
        changePrice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onListener.Listener(index);
            }
        });
        return convertView;
    }

    public void Click(OnListener onlistener) {
        this.onListener = onlistener;
    }

    public interface OnListener {
        void Listener(int index);
    }


    static
    class ViewHolder {
        @BindView(R.id.priceQuery_name)
        TextView priceQueryName;
        @BindView(R.id.sku_name)
        TextView skuName;
        @BindView(R.id.linear_sku)
        LinearLayout linearSku;
        @BindView(R.id.view_sku)
        View viewSku;
        @BindView(R.id.priceQuery_freightNumber)
        TextView priceQueryFreightNumber;
        @BindView(R.id.priceQuery_code)
        TextView priceQueryCode;
        @BindView(R.id.priceQuery_storeRetail)
        TextView priceQueryStoreRetail;
        @BindView(R.id.priceQuery_storeMember)
        TextView priceQueryStoreMember;
        @BindView(R.id.linear_storeMember)
        LinearLayout linearStoreMember;
        @BindView(R.id.priceQuery_mall)
        TextView priceQueryMall;
        @BindView(R.id.linear_mall)
        LinearLayout linearMall;
        @BindView(R.id.priceQuery_mallMember)
        TextView priceQueryMallMember;
        @BindView(R.id.linear_mallMember)
        LinearLayout linearMallMember;
        @BindView(R.id.priceQuery_plus)
        TextView priceQueryPlus;
        @BindView(R.id.linear_plus)
        LinearLayout linearPlus;
        @BindView(R.id.priceQuery_changePrice)
        TextView priceQueryChangePrice;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
